package com.example.lottery.service.activitystatus.operater;

import com.example.lottery.dao.datobject.ActivityDO;
import com.example.lottery.dao.datobject.ActivityPrizeDO;
import com.example.lottery.dao.mapper.ActivityMapper;
import com.example.lottery.dao.mapper.ActivityPrizeMapper;
import com.example.lottery.service.dto.ConvertActivityStatusDTO;
import com.example.lottery.service.enums.ActivityPrizeStatusEnum;
import com.example.lottery.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus=convertActivityStatusDTO.getTargetPrizeStatus();
        if(activityId==null||targetStatus==null
        ||prizeId==null){
            return false;
        }
        ActivityPrizeDO activityPrizeDO=activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(activityPrizeDO==null){
            return false;
        }
        //如果当前的活动状态与传入的活动状态一致 也不进行处理
        if(targetStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())){
            return false;
        }
        return true;

    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus=convertActivityStatusDTO.getTargetPrizeStatus();
        try{
            activityPrizeMapper.updateStatus(activityId,prizeId,targetPrizeStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
